/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoexam.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoServantName;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoServantNameService;
import com.lczyfz.campusinfo.modules.campusinfopay.entity.CampusinfoPay;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoServant;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoServantService;

import java.util.List;

/**
 * 公务员考试报名Controller
 * @author zms
 * @version 2018-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoexam/campusinfoServant")
public class CampusinfoServantController extends BaseController {

	@Autowired
	private CampusinfoServantService campusinfoServantService;
	@Autowired
	private CampusinfoServantNameService campusinfoServantNameService;
	@Autowired
	private CampusinfoPayService campusinfoPayService;
	@ModelAttribute
	public CampusinfoServant get(@RequestParam(required=false) String id) {
		CampusinfoServant entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoServantService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoServant();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfoexam:campusinfoServant:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoServant campusinfoServant, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoServant> page = campusinfoServantService.findPage(new Page<CampusinfoServant>(request, response), campusinfoServant);
		List<CampusinfoServant> campusinfoServants = campusinfoServantService.findList(campusinfoServant);
		model.addAttribute("campusinfoServants", campusinfoServants);
		model.addAttribute("page", page);
		return "modules/campusinfoexam/campusinfoServantList";
	}

	@RequiresPermissions("campusinfoexam:campusinfoServant:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoServant campusinfoServant, Model model) {
		CampusinfoServantName campusinfoServantName = new CampusinfoServantName();
		List<CampusinfoServantName> campusinfoServantNames = campusinfoServantNameService.findList(campusinfoServantName);
		model.addAttribute("campusinfoServantNames",  campusinfoServantNames);
		model.addAttribute("campusinfoServant", campusinfoServant);
		return "modules/campusinfoexam/campusinfoServantForm";
	}

	@RequiresPermissions("campusinfoexam:campusinfoServant:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoServant campusinfoServant, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoServant)){
			return form(campusinfoServant, model);
		}
		campusinfoServantService.save(campusinfoServant);
		addMessage(redirectAttributes, "保存公务员考试报名成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoexam/campusinfoServant/?repage";
	}
	
	@RequiresPermissions("campusinfoexam:campusinfoServant:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoServant campusinfoServant, RedirectAttributes redirectAttributes) {
		campusinfoServantService.delete(campusinfoServant);
		addMessage(redirectAttributes, "删除公务员考试报名成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoexam/campusinfoServant/?repage";
	}
	@RequiresPermissions("campusinfoexam:campusinfoServant:edit")
	@RequestMapping(value = "status")
	public String status(CampusinfoServant campusinfoServant, RedirectAttributes redirectAttributes) {
		campusinfoServant.setStatus(CampusinfoOrderStat.DONE.getValue());
		campusinfoServantService.status(campusinfoServant);
		CampusinfoPay campusinfoPay = new CampusinfoPay();
		campusinfoPay.setId(campusinfoServant.getOrderCode());
		campusinfoPay.setStat(CampusinfoOrderStat.DONE.getValue());
		campusinfoPayService.status(campusinfoPay);
		addMessage(redirectAttributes, "修改公务员支付状态");
		return "redirect:" + Global.getAdminPath() + "/campusinfoexam/campusinfoServant/?repage";
	}

}